<template>
    <el-container class="home-container">
            <el-header>
                <span>游戏后台管理系统</span>
                <el-button type="info" @click='logout'>退出</el-button>
            </el-header>
        <el-container>
            <el-aside :width="flag? '64px':'200px'">
                <div class='toggle-collapse' @click='toggle'>|||</div>
                <el-menu
                     background-color="#333744"
                    text-color="#fff"
                    active-text-color="#ffd04b"
                    :collapse='flag'
                    :collapse-transition=false
                    unique-opened
                    router
                    :default-active='defaultActive'
                    >
                    <el-submenu :index="''+item.id" v-for='item in menuList' :key='item.id'>
                     <template slot="title">
                       <i :class="incontObj[item.id]"></i>
                       <span>{{item.authName}}</span>
                     </template>
                       <el-menu-item :index="'/' + val.path" v-for='val in item.children' :key='val.id' @click='defaultClick("/" +val.path)'>
                       <template slot="title">
                           <!-- 图标 -->
                           <i class="el-icon-menu"></i>
                           <!-- 文本 -->
                           <span>{{val.authName}}</span>
                       </template>
                       </el-menu-item>
                     </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <!-- 路由占位符 -->
                <router-view/>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      incontObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      flag: false,
      defaultActive: ''
    }
  },
  methods: {
    //   退出功能
    logout () {
      if (confirm('您确定要退出么？')) {
        //   将sessionStorage清除token
        window.sessionStorage.removeItem('token')
        this.$router.push('/login')
      }
    },
    async getMenu () {
      const data = await this.$http.get('menus')
      this.menuList = data.data
      console.log(data.data)
    },
    toggle () {
      this.flag = !this.flag
    },
    defaultClick (pathActive) {
    // 将路径放到defaultActive当中使得标签高亮显示
      this.defaultActive = pathActive
      // 获取点击对象路径保存到sessionStorage中
      window.sessionStorage.setItem('pathActive', pathActive)
    }
  },
  created () {
    //   在created钩子函数中获取数据
    this.getMenu()
    // 获取pathActive
    this.defaultActive = window.sessionStorage.getItem('pathActive')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header{
    position: relative;
  background-color:#373D41;
}
.el-header span {
    position: absolute;
    font-size: 25px;
    font-weight: 700;
    transform: translate(10px,12px);

}
.el-aside{
  background-color:#333744;
}
.el-main{
  background-color:#eaedf1;
}
.el-button{
    float:right;
    margin-top:10px;
}
.iconfont {
    margin-right: 10px;
}
.toggle-collapse {
    background-color: #333744;
    letter-spacing: 3px;
    text-align: center;
    color:#fff;
    cursor: pointer;
}
.el-menu {
    border:0;
}
</style>
